package org.net{
	
	import flash.display.Sprite;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.events.Event;
	
	import org.abcFrame.core.*;
	import org.net.*;
	import org.enmu.*;
	
	public class AbcLoaderStream extends Sprite {
		
		private var path:String
		private var connection:NetConnection;
		private var stream:NetStream;
		
		private var responder:AbcResponder
		
		public function AbcLoaderStream(path:String,responder:AbcResponder) {
			
			this.path=path
			this.responder=responder
			
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			connection.connect(null);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void {
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
					AbcEventDispatcher.instance.dispatcher(AbcEventType.SOUNDEVENT,"Stream not found: "+path)
					break;
			}
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {

			AbcEventDispatcher.instance.dispatcher(AbcEventType.SOUNDEVENT,"securityErrorHandler: "+path+event.text)
				
		}
		
		private function connectStream():void {
			
			var stream:NetStream = new NetStream(connection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.client = new CustomClient();
			var video:Video = new Video();
			video.attachNetStream(stream);
			stream.play(path);
			addChild(video);
			
		}
		
	}
}

class CustomClient {
	public function onMetaData(info:Object):void {
		trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
	}
	public function onCuePoint(info:Object):void {
		trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
	}
}

